package com.home766.controller.admin;

import com.home766.dto.AreaDto;
import com.home766.dto.DictionaryDTO;
import com.home766.entity.Appointment;
import com.home766.entity.Shops;
import com.home766.service.AppointmentService;
import com.home766.service.IDictionaryService;
import com.home766.service.ShopsService;
import net.biz.Message;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.admin.BaseController;
import net.biz.entity.Area;
import net.biz.service.AreaService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/appointment")
public class AppointmentController extends BaseController {
	
	@Resource(name="appointmentServiceImpl")
	private AppointmentService appointmentService;
	@Resource(name="dictionaryServiceImpl")
	private IDictionaryService iDictionaryService;
	@Resource(name="areaServiceImpl")
	private AreaService areaService;
	@Resource(name="shopsServiceImpl")
	private ShopsService shopsService;
	@RequestMapping("/list")
	public String appointmentPage(Appointment appointment, Pageable pageable, ModelMap modelMap, String sort, String properties){
		List<DictionaryDTO> dc = iDictionaryService.searchChildren(92L);
		Page<Appointment> page = appointmentService.findList(appointment, pageable,properties);
		List<Area> parentList = areaService.findRoots();
		modelMap.addAttribute("page",page);
		modelMap.addAttribute("sort",sort);
		modelMap.addAttribute("dc",dc);
		modelMap.addAttribute("parentList",parentList);
		return "/admin/appointment/list";
	}
	/***
	 * 批量删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(Long id[], RedirectAttributes redirectAttributes){
		for(Long i : id){
			appointmentService.delete(i);
			addFlashMessage(redirectAttributes,new Message(Message.Type.success,"删除成功"));
		}
		return "redirect:list.jhtml";
	}
	/***
	 * 处理
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("/update")
	public String update(Long id, Long withSingle, Long shopsId, RedirectAttributes redirectAttributes){
		Appointment appointment = appointmentService.find(id);
		appointment.setHandle(1);
		appointment.setWithSingle(withSingle);
		appointment.setShopsId(shopsId);
		appointmentService.update(appointment);
		addFlashMessage(redirectAttributes,new Message(Message.Type.success,"修改成功"));
		return "redirect:list.jhtml";
	}
	/***
	 * 根据省/市查询下级
	 * @param provinceId
	 * @return
	 */
	@RequestMapping(value = "/getChildrenByParentId")
	@ResponseBody
	public List<AreaDto> getChildrenByParentId(Long provinceId){
		List<AreaDto> getChildrenList = areaService.findChildren(provinceId);
		return getChildrenList;
	}
	/***
	 * 查询服务门店
	 * @param areaId
	 * @return
	 */
	@RequestMapping("/getShopsByAreaId")
	@ResponseBody
	public List<Shops> getShopsByAreaId(Long areaId){
		List<Shops> shopsList = shopsService.getShopsByAreaId(areaId,false,1L);
		return shopsList;
	}
}
